home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / errmsg.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  182 lines

  1. /************************************************************************
  2.  * $Id: errmsg.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  *  Error handling routines.
  9.  *
  10.  *  Messages are divided into graphics and non-graphics types.
  11.  *  For graphical error messages, a user input may be demanded,
  12.  *  while for non-graphical messages, a string is printed and
  13.  *  does nothing else.
  14.  *
  15.  *  The graphical output routine must have the following form:
  16.  *    void (*gmout)(const char *, const char *, const char *, int);
  17.  ***********************************************************************/
  18. #if !defined(lint) && defined(F_ID)
  19. char *id_errm = "$Id: errmsg.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  20. #endif
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include <errno.h>
  28.  
  29. #include "ulib.h"
  30. extern int errno;        /* system error no            */
  31.  
  32. #define MAXESTR 1024        /* maximum error string len   */
  33.  
  34. /*
  35.  * msg_threshold controls amount of message to print
  36.  * 0: only error      1: error and warning
  37.  * 2: info            3: lots of messages
  38.  */
  39.  
  40. /************ Local variables ****************************************/
  41.  
  42. static FILE *errlog;        /* where the msg is going       */
  43. static ML_t threshold;        /* current threshold            */
  44. static ML_t req_level;        /* requested message level      */
  45. static const char *file;    /* source file name             */
  46. static int lineno;        /* line no. in that file        */
  47. static int gout;        /* if demand graphics           */
  48. static int f_l;            /* true if func and line output */
  49. static Gmsgout_ gmout;
  50.  
  51. /******* set up where err is gonna go **************/
  52. void
  53. set_err_msg_fp(FILE * fp)
  54. {
  55.     if (fp)
  56.     errlog = fp;
  57. }
  58.  
  59. /***** Message levels  ****************************/
  60. void
  61. set_msg_threshold(ML_t mlevel)
  62. {
  63.     threshold = mlevel;
  64. }
  65.  
  66. /***** Graphics output routine ********************/
  67. void
  68. set_err_msg_func(Gmsgout_ errf)
  69. {
  70.     gmout = errf;
  71. }
  72.  
  73. /******* If output filename and line number ******/
  74. void
  75. set_err_msg_brief(int b)
  76. {
  77.     f_l = !b;
  78. }
  79.  
  80.  
  81.  
  82. ErrFunc_ efp_;            /* global pointer to shut up lint */
  83.  
  84.  
  85. /*******************************************************************
  86.  * generate two strings that contain where and why an error occured
  87.  *********************************************************************/
  88. /* VARARGS2 */
  89. static void
  90. P_errmsg(const char *func, const char *fmt,...)
  91. {
  92.     va_list args;
  93.     char *where, *why, *pp, line[20];
  94.     static char emsg[MAXESTR + 1];
  95.  
  96.     if (!errlog)
  97.     errlog = stderr;
  98.  
  99.     /* if there is nothing to do, do nothing ! */
  100.     if (req_level >= threshold && (!gout || !gmout))
  101.     return;
  102.  
  103. /******** find out where the error occured *************/
  104.  
  105.     if (f_l)
  106.       {
  107.       strcpy(line, lineno > 0 ? itoa(lineno) : "?");
  108.  
  109.       where = (func && *func) ?
  110.           vstrcat("In ", func, " [", file, " ", line, "]", (char *) 0) :
  111.           vstrcat("In ", file, ", ", line, ":", (char *) 0);
  112.       }
  113.     else
  114.       {
  115.       line[0] = '\0';
  116.       where = (func && *func) ?
  117.           vstrcat(func, "-", (char *) 0) :
  118.           strdup(" ");
  119.       }
  120.  
  121.     /************** and why ********************************/
  122.  
  123.     emsg[0] = '\0';
  124.     why = 0;
  125.  
  126.     /* parse the fmt */
  127.     if (fmt && *fmt)
  128.       {
  129.       va_start(args, fmt);
  130.       (void) vsprintf(emsg, fmt, args);
  131.       va_end(args);
  132.       }
  133.  
  134.     /* check if there is any system errors */
  135.  
  136.     if ((pp = errno ? strerror(errno) : 0))
  137.     strcat(strcat(emsg, "--"), pp);
  138.  
  139.     why = emsg;
  140.  
  141. /*
  142.  * have gotten the message, where and why, show it
  143.  */
  144.     if (req_level < threshold)
  145.     fprintf(errlog, "%s %s\n", where, why);
  146.  
  147.     if (gout && gmout)
  148.     gmout("Warning:", where, why, 0);
  149.  
  150.     free_vstrcat(where);
  151.  
  152.     /* reset system errors */
  153.     errno = 0;
  154.     return;
  155. }
  156.  
  157. /*********************************************************************
  158.  * get the line number in file where error occurred. gui indicates
  159.  * if graphics output function is to be called
  160.  ********************************************************************/
  161.  
  162. ErrFunc_
  163. whereError(int gui, ML_t level, const char *f, int l)
  164. {
  165.     file = f;
  166.     lineno = l;
  167.     req_level = level;
  168.     gout = gui;
  169.     return P_errmsg;
  170. }
  171.  
  172. #ifdef TEST
  173. extern void TC_continue(const char *, const char *, const char *, int);
  174. main()
  175. {
  176.     set_err_msg_fp(stderr);
  177.     Bark("ThisisMain", "error1");
  178.     M_err("ThisisMain", "error1");
  179. }
  180.  
  181. #endif
  182.